﻿var poly;
var lMovBus;

function VerElementosPaginaVisualizar() {
    //alert("dibujar");
    var varURL = getGET();
    $("#panel").append("<font color='#FFFFFF'>Control de buses de " + varURL.parRuta + "</font><br/><br/><ul class='list-group' id='tCtrlBuses'></ul>");
   
    DibujarRuta();
    VerBusesRuta(varURL.parRuta);
}

function DibujarRuta() {
    var polyOptions = {
        strokeColor: SelectColorRuta(10),
        strokeOpacity: 1.0,
        strokeWeight: 1,
    };
    poly = new google.maps.Polyline(polyOptions);
    poly.setMap(map);
    var varURL = getGET();
    var parametro = {
        NomRuta: "'" + varURL.parRuta + "'"
    };
    var DirSer = localStorage.getItem("DirSerUsu");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/getCoordenadasRuta";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            var lpuntos = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            $.each(lpuntos, function (index, item) {
                var path = poly.getPath();
                path.push(new google.maps.LatLng(item.lat, item.lon));
            });
        },
        error: function (jqXHR, status, error) {
            //alert(error + "-" + jqXHR.responseText);
        }
    });
}

function VerBusesRuta(nombreRuta) {
    var lati;
    var long;
    var parametro = {
        NomRuta: "'" + nombreRuta + "'"
    };
    var DirSer = localStorage.getItem("DirSerUsu");
    var ruta = DirSer + "/Servicios/ServiciosJusTime.asmx/ObtenerCoorenadas";
    $.ajax({
        type: "GET",
        url: ruta,
        data: parametro,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            lMovBus = (typeof result.d) == 'string' ? eval('(' + result.d + ')') : result.d;
            deleteMarkers();
            $("#tCtrlBuses").html("");
            $.each(lMovBus, function (index, item) {
                $("#tCtrlBuses").append("<li class='list-group-item' style='background:#DAA520'><span class='badge'>" + item.Velocidad + " Km/h</span>" + item.Placa + "</li>");
                lati = parseFloat(item.Latitud);
                long = parseFloat(item.Longitud);
                marker = new google.maps.Marker({
                    map: map,
                    position: new google.maps.LatLng(lati, long),
                    title: item.Placa + "  " + item.Velocidad + " Km/h",
                    icon: "Recursos/bus.png"
                });
                markers.push(marker);
            });
        },
        error: function (jqXHR, status, error) {
        }
    });
    timer = setTimeout("VerBusesRuta('" + nombreRuta + "')", 5000);
}

function SelectColorRuta(Sele) {
    switch(Sele) {
        case 0:
            return "#000000";
            break;
        case 1:
            return "#0000FF";
            break;
        case 2:
            return "#A52A2A";
            break;
        case 3:
            return "#7FFF00";
            break;
        case 4:
            return "#DC143C";
            break;
        case 5:
            return "#006400";
            break;
        case 6:
            return "#8B0000";
            break;
        case 7:
            return "#483D8B";
            break;
        case 8:
            return "#9400D3";
            break;
        case 9:
            return "#B22222";
            break;
        case 10:
            return "#008000";
            break;
        case 11:
            return "#4B0082";
            break;
        case 12:
            return "#7CFC00";
            break;
        case 13:
            return "#20B2AA";
            break;
        case 14:
            return "#00FF00";
            break;
        case 15:
            return "#800000";
            break;
        case 16:
            return "#0000CD";
            break;
        case 17:
            return "#008080";
            break;
        case 18:
            return "#9ACD32";
            break;
        case 19:
            return "#FF6347";
            break;
        case 20:
            return "#A0522D";
            break;


    }

}

function CalcularDistancia() {
    var PriBus = $("#tPrimerBus").val();
    var SegBus = $("#tSegundoBus").val();
    var unoLat;
    var unoLon;
    var dosLat;
    var dosLon;
    $.each(lMovBus, function (index, item) {
        if (item.Placa == PriBus) {
            unoLat = parseFloat(item.Latitud);
            unoLon = parseFloat(item.Longitud);
        }
        if (item.Placa == SegBus) {
            dosLat = parseFloat(item.Latitud);
            dosLon = parseFloat(item.Longitud);
        }
    });
    var BusUNO = new google.maps.LatLng(unoLat, unoLon);
    var BusDOS = new google.maps.LatLng(dosLat, dosLon);
    var distancia = google.maps.geometry.spherical.computeDistanceBetween(BusUNO, BusDOS);
    $("#tCalculoDistancia").text("Distancia aproximada: " + distancia + " Mts");
}